﻿layui.use(['table', 'laypage', 'layer', 'jquery'], function () {
    var table = layui.table;
    var laypage = layui.laypage;
    var $ = layui.$;
    var layer = layui.layer; // 添加layui弹出层模块

    var pageSize = 15; // 每页显示条数
    //获取列表
    function getListData(pageIndex) {
        //获取查询条件
        var earNumber = $('#earNumber').val();

        //ajax后台交互
        $.ajax({
            url: 'http://10.31.60.18:8000/BeginningGetRout/api/BeginningRead/BeginningShow',
            type: 'get',
            dataType: 'json',
            //参数
            data: {
                pageIndex: pageIndex,
                pageSize: pageSize,
                earNumber: earNumber,
                Species: 2,
            },
            success: function (res) {
                table.render({
                    //挂载的表格id
                    elem: '#ID-table-demo-setRowChecked',
                    cols: [
                        [
                            { type: 'checkbox' },

                            { field: 'earNumber', title: '羊只耳号' },
                            { field: 'buildings', title: '栋舍' },
                            { field: 'columns', title: '栏位' },
                            { field: 'sheepNumber', title: '只数' },
                            { field: 'weight', title: '重量' },
                            { field: 'createBy', title: '创建人' },

                            { field: 'createDate', title: '创建时间', templet: function (data) { return data.createDate.substring(0, 10) } },

                            { title: '操作', toolbar: '#barDemo' }
                        ]
                    ],
                    data: res.datas
                });

                //分页
                laypage.render({
                    //挂载的分页id
                    elem: 'pagesbox',
                    count: res.totalCount,
                    limit: pageSize,
                    curr: pageIndex,
                    layout: ['count', 'prev', 'page', 'next', 'skip'],
                    jump: function (obj, first) {
                        if (!first) {
                            getListData(obj.curr);
                        }
                    }
                });
            }
        });
    }

    $('#searchBtn').on('click', function () {
        getListData(1);
    });

    // 监听工具条，实现删除功能
    table.on('tool(ID-table-demo-setRowChecked)', function (obj) {
        var data = obj.data;
        if (obj.event == 'delete') {
            layer.confirm('确定删除吗？', function (index) {
                deleteData(data.beginningId); // 传递正确的数据项ID
                layer.close(index);
            });
        }
    });

    // 批量删除
    $(".delAll_btn").click(function () {
        var checkStatus = table.checkStatus('ID-table-demo-setRowChecked'),
            data = checkStatus.data,
            beginningId = "";
        if (data.length > 0) {
            for (var i in data) {
                beginningId += data[i].beginningId + ",";
            }
            beginningId = beginningId.substring(0, beginningId.length - 1);
            console.log(beginningId);
            layer.confirm('确定删除选中的记录？', { icon: 3, title: '提示信息' }, function (index) {
                $.ajax({
                    url: `http://10.31.60.18:8000//BeginningSetRout/api/Beginning/BeginninBatchDelete?BeginningId=${beginningId}`,
                    type: 'delete',
                    success: function (res) {
                        if (res.code == 200) {
                            layer.msg("批量删除成功");
                            getListData(1);
                        } else {
                            layer.msg("批量删除失败");
                        }
                    },
                    error: function () {
                        layer.msg('批量删除请求失败');
                    }
                });
                layer.close(index);
            });
        } else {
            layer.msg("请选择需要删除的记录");
        }
        console.log(checkStatus);
    });

    // 编辑按钮点击事件
    table.on('tool(ID-table-demo-setRowChecked)', function (obj) {
        var data = obj.data; // 获取当前行数据
        var BeginningId = data.beginningId; // 获取当前行数据的唯一标识符
        if (obj.event === 'edit') {
            var url = `/Beginning/SpermatophoreUpdate?BeginningId=${BeginningId}`;
            location.href = url; // 点击编辑按钮时跳转到修改页面
        }
        if (obj.event == 'delete') {
            layer.confirm('确定删除吗？', function (index) {
                deleteData(data.beginningId); // 传递正确的数据项ID
                layer.close(index);
            });
        }
    });
    // 单删除
    function deleteData(beginningId) {
        $.ajax({
            url: `http://10.31.60.18:8000//BeginningSetRout/api/Beginning/BeginninDelete?BeginningId=${beginningId}`,
            type: 'delete',
            success: function (res) {
                if (res.code == 200) {
                    layer.msg('删除成功');
                    getListData(1);
                } else {
                    layer.msg('删除失败');
                }
            },
            error: function () {
                layer.msg('删除请求失败');
            }
        });
    }

    // 初始化加载数据
    $(function () {
        getListData(1);
    })
});